// pages/game/game.js
import LAN from '../../utils/lan';

const app = getApp();

const innerAudioContextGame = wx.createInnerAudioContext({useWebAudioImplement: false});
innerAudioContextGame.src = 'cloud://cloud1-8grlkyuf1058902e.636c-cloud1-8grlkyuf1058902e-1322046947/百石元 - 猪突猛進.mp3';
// 设置自动播放
innerAudioContextGame.autoplay = false;
// 设置循环播放
innerAudioContextGame.loop = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:'',
    nickName:'',
    remoterInfo:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getOpenerEventChannel().once('args',res=>{
      const { avatarUrl, nickName } = res.localInfo;
      const gameVs = this.selectComponent('#game_vs');
      console.log("IsRobot",res.IsRobot)
      gameVs.setIsRobot(res.IsRobot);
      this.setData({IsRobot:res.IsRobot});

      const gameChess = this.selectComponent('#game_vs').selectComponent('#FTWindow');
      console.log("res.IsOwner",res.IsOwner);
      if(res.IsRobot&&res.Barrier!=undefined){
        gameChess.setBarrier(res.Barrier);
        console.log("Load",res.Barrier)
      }
      gameChess.setIsOwner(res.IsOwner);
      gameChess.setIsRobot(res.IsRobot);
      this.setData({
        avatarUrl,
        nickName
      });
      if (res.remoterInfo) {
        this.setData({
          remoterInfo:res.remoterInfo
        })
        // this.data.remoterInfo = res.remoterInfo;
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.setKeepScreenOn({
      keepScreenOn: true,
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("进入Game onShow")
    // 设置音频文件的路径

  // 开始播放音乐
  innerAudioContextGame.play();
  // 监听播放事件
  innerAudioContextGame.onPlay(() => {
  console.log('开始播放');
  });
  // 监听错误事件
  innerAudioContextGame.onError((res) => {
    console.log(res.errMsg);
    console.log(res.errCode);
  });
  innerAudioContextGame.onWaiting(
    (res)=>{
      console.log("没加载好")
    }
)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    innerAudioContextGame.stop();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.setKeepScreenOn({
      keepScreenOn: false,
    })
    innerAudioContextGame.stop();

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onClick: function (e) {
    // console.log(e.target.id);
    var id = e.target.id;
    if (id == "btn1") {
      let gameVs = this.selectComponent('#game_vs');
      if(this.data.IsRobot){
        gameVs.resetRobot();
      }else{
        gameVs.resetGame();
      }
    } else if (id == "btn2") {
      app.showModal({
        title: '游戏说明',
        content: '该游戏为回合制战斗游戏，需要两个人参与一起玩。\n祝您游戏愉快！',
        showCancel: false
      })
    }else if (id == "btn3") {
      wx.navigateTo({
        url: '/pages/index/index',
        success(res){
          console.log("返回首页");
        }
      });
    }else if (id == "btn4") {
      wx.navigateTo({
        url: '/pages/gameChoose/gameChoose',
        success(res){
          console.log("回到选择关卡页面")
        }
      });
    }
  },
})